//工程树对象
var treeData = null;
//默认的工程树数据
var treeInitData = [
    { "name": "矢量", id: 1, type: 'group', checked: false },
    { "name": "模型", id: 2, type: 'group', checked: false },
    { "name": "街景", id: 3, type: 'group', checked: false },
    { "name": "地形", id: 4, type: 'group', checked: false },
    { "name": "影像", id: 5, type: 'group', checked: false }
];

$(function () {
    //底部菜单操作 鼠标hover 展示菜单
    $('.con_bottom').on('mouseover', '.btn_area>ul>li', function () {
        $(this).find('.btn_area_div').removeClass('hide').show()
    });
    $('.con_bottom').on('mouseout', '.btn_area>ul>li', function () {
        $('.btn_area_div').hide();
    })

    window.sgworld = new SmartEarth.SGWorld("cesiumContainer", Cesium, {
        // url: "/static/IMG/earth.jpg",
        // baseLayerPicker: true,
        // imageryProviderViewModels: [],
        // terrainProviderViewModels: []
        //需要进行可视化的数据源的集合
        animation: false, //是否显示动画控件
        shouldAnimate: true,
        homeButton: false, //是否显示Home按钮
        fullscreenButton: false, //是否显示全屏按钮
        baseLayerPicker: false, //是否显示图层选择控件
        geocoder: false, //是否显示地名查找控件
        timeline: false, //是否显示时间线控件
        sceneModePicker: true, //是否显示投影方式控件
        navigationHelpButton: false, //是否显示帮助信息控件
        infoBox: false, //是否显示点击要素之后显示的信息
        requestRenderMode: true, //启用请求渲染模式
        scene3DOnly: false, //每个几何实例将只能以3D渲染以节省GPU内存
        sceneMode: 3, //初始场景模式 1 2D模式 2 2D循环模式 3 3D模式  Cesium.SceneMode
        fullscreenElement: document.body, //全屏时渲染的HTML元素 暂时没发现用处
        //天地图是官方开元的地图，不需要密钥
        imageryProvider: new Cesium.UrlTemplateImageryProvider({
            url: "http://mt1.google.cn/vt/lyrs=s&hl=zh-CN&x={x}&y={y}&z={z}&s=Gali"
        })
    }, null, function () {
    });
    window.Viewer = sgworld._Viewer;
    //如果是新建的模板则弹出工程树
    if (!parent.id) {
        treeStart(treeInitData)
    }

    //数据
    switch (parent.datatype) {
        case 'point':
            var dx = sgworld.Creator.createLabelPointGeoJsonFeatureLayer("", '../JSON/point.json', {
                far: 900000,
                font_family: "SimSun",
                font_size: 16,
                heightReference: 2,
                horizontalOrigin: 0,
                layertype: "labellayer",
                level_max: 21,
                level_min: 0,
                near: 0,
                outlineWidth: 5,
                pointHeight: "[height]",
                showBackground: false,
                stylet: "2",
                text: "[name]",
                verticalOrigin: -1,
            }, "0", true, function (data) {
                sgworld.Navigate.flyToObj(data);
            });
            break
        case 'polyline':
            var dx = sgworld.Creator.createStreetscapeGeoJsonFeatureLayer("", '../JSON/line.json', {}, "0", true, function (data) {
                sgworld.Navigate.flyToObj(data);
            });
            break
        case 'polygon':
            var dx = sgworld.Creator.createPolygonMapGeoJsonFeatureLayer("", '../JSON/plane.json', {
                extrudedHeight: "10",
                extrudedHeightReference: 0,
                far: "7000",
                fill: "rgba(237, 7, 7, 0.9)",
                heightReference: 2,
                layertype: "polygonlayer",
                level_max: 21,
                outline: false,
                outlineColor: "rgba(237, 7, 7, 0.9)",
                outlineWidth: 1,
            }, "0", true, function (data) {
                //sgworld.Navigate.flyToObj(data);
                sgworld.Navigate.flyToPosition(-2610164.0467195935, 4748666.325820415, 3372496.2552132295);
            });
            break
    }
})


//模板
//工具模板
function setToolNav(data) {
    $('.tool').remove();
    if (data && data.length > 0) {
        $('.con_bottom').show();
        for (var i = 0, l = data.length; i < l; i++) {
            switch (data[i].type) {
                case '图层':
                    tag = 'layerHandleClick'
                    break
                case '属性':
                    tag = 'propHandle'
                    break
                case '操作':
                    tag = 'funcHandle'
                    break
                case '结果':
                    tag = 'resHandle'
                    break
            }
            $('body').append('<div onclick="' + tag + '(this)" title="' + data[i].type + '" style="left:' + data[i].left + 'px;top:' + data[i].top + 'px;right:' + data[i].right + 'px" class="tool"><img src="' + (data[i].icon || parent.parent.m_init_icon) + '"></div>')
        }
    }
}


//功能模板
function setFuncNav(data) {
    var html = '';
    $('.btn_area').width(data.length * 72 + 'px');
    for (var i = 0, l = data.length; i < l; i++) {
        html += '<li>';
        if (data[i].tag && data[i].tag != '') {
            html += '<span class="pic_small" onclick=' + data[i].tag + '("' + data[i].tip + '")><img src="' + (data[i].icon || parent.parent.m_init_icon) + '" class="hand" /></span>\n' +
                '<p class="jd_title">' + data[i].type + '</p>'
        } else {
            html += '<span class="pic_small"><img src="' + (data[i].icon || parent.parent.m_init_icon) + '" class="hand" /></span>\n' +
                '<p class="jd_title">' + data[i].type + '</p>'
        }

        //存在二级菜单
        var childHtml = '';
        if (data[i].children && data[i].children.length > 0) {
            var height = 37 * (data[i].children.length);
            html += '<div class="btn_area_div hide">' +
                '<span style="display: block;" class="arrow4">\n' +
                '<span class="arrow4_1"></span>\n' +
                '<span class="arrow4_2"></span>\n' +
                '<span class="content" style=" height:' + height + 'px">' +
                '<span>\n' +
                '<ul>'
            for (var k = 0, j = data[i].children.length; k < j; k++) {
                childHtml += '<li onclick=' + data[i].children[k].tag + '("' + data[i].children[k].tip + '")><img class="icon_level" src="' + (data[i].children[k].icon || parent.parent.m_init_icon) + '"/>' + data[i].children[k].type + '</li>'
            }
            html += childHtml + '</ul></span></span></span></div>'
        } else {
            html += '<li>'
        }

    }
    $('.btn_area ul').html(html);
}

//模板中触发图层事件
var treeIndex = null;
function layerHandle() {
    treeIndex = layer.open({
        skin: 'other-class',
        type: 2,
        title: '工程配置',
        area: ['350px', '500px'],
        fixed: false, //不固定
        shade: false,
        resize: false,
        content: 'html/layer/layer.html',
        offset: ['80px', '20px'],
        anim: -1,
        maxmin :true,
        closeBtn :false,
        success:function(layero,index){
            layero.find('.layui-layer-max').hide()
        },
        min:function(layero,index){
            layero.find('.layui-layer-max').show()
        },
        restore :function(layero,index){
            layero.find('.layui-layer-max').hide()
        }
    })
}

//点击图层触发图层事件
function layerHandleClick(obj){
    //获取当前按钮的位置
    var mid = $(window).width() / 2;
    lPosi = $(obj).css('left') > mid ? 'right' : 'left';
    lLeft = lPosi === 'left' ? $(obj).css('left') : $(obj).css('right')
    lTop = $(obj).css('top');

    fnLayerOpen('工程树', 'html/layer/layer.html', 400, 600);
}

//工程树初始化
//数据库保存的styletype和servertype属性与返回的id进行进行关联存储信息
var sqllayerObj = null;
function treeInit(data) {
    if (data) {
        sqllayerObj = JSON.parse(data).layerObj;
        treeStart(JSON.parse(data).ztree)
    } else {
        treeStart(treeInitData)
    }
}

//初始化球飞行指定位置
function flyPosi(data) {
    if (data) {
        sgworld.Navigate.flyToPointsInterest({
            destination: Cesium.Cartesian3.fromDegrees(data.x,data.y, data.z),
            orientation: {
                heading: Cesium.Math.toRadians(data.yaw),
                pitch: Cesium.Math.toRadians(data.pitch),
                roll: Cesium.Math.toRadians(data.roll)
            }
        });
    }
}